%%%% GoF pictures

disp('Demo for a Kolmogorov-Smirnov Goodness of fit test')
  lw = 2;
  set(0, 'DefaultAxesFontSize', 15);
  fs = 17;
  msize = 4;

%Demo of KS Test.
%------------------------------------------------------------------
%Galaxy data
%
%This interesting data set was first described in Roeder (1990), and
%subsequently analysed under different mixture models by several authors.
%It consists of the velocities of 82 distant galaxies, diverging from our
%own galaxy.
%
galaxy = [ 9.172  9.350  9.483  9.558  9.775 10.227 10.406 16.084 16.170 18.419 ...
18.552 18.600 18.927 19.052 19.070 19.330 19.343 19.349 19.440 19.473 ...
19.529 19.541 19.547 19.663 19.846 19.856 19.863 19.914 19.918 19.973 ...
19.989 20.166 20.175 20.179 20.196 20.215 20.221 20.415 20.629 20.795 ...
20.821 20.846 20.875 20.986 21.137 21.492 21.701 21.814 21.921 21.960 ...
22.185 22.209 22.242 22.249 22.314 22.374 22.495 22.746 22.747 22.888 ...
22.914 23.206 23.241 23.263 23.484 23.538 23.542 23.666 23.706 23.711 ...
24.129 24.285 24.289 24.366 24.717 24.990 25.633 26.960 26.995 32.065 ...
32.789 34.279 ];
figure(1)
hist(galaxy,40);
h = findobj(gca,'Type','patch');
%set(gcf,'Color',[1,0.4,0.6])
set(h,'FaceColor',[0.5,0.5,0.5],'EdgeColor','k')
print -deps 'C:\NPBook\GoF\galaxy1.eps'

 %Hypothesis 01: Galaxy data is normal mu=21, var=21. [by moment matching]
 lefts = cdf_empirical( galaxy - 10^(-10), galaxy );
 rights = cdf_empirical( galaxy , galaxy );
 theo1 = cdf_nor( galaxy, 21, sqrt(21) );
 figure(2)
 plot( lefts  ,'k.','markersize', msize)
 hold on
 plot(rights ,'k*','markersize', msize)
 plot(theo1,'k--', 'linewidth', 3 )
 axis tight
 print -deps 'C:\NPBook\GoF\galaxy2.eps'
 Dn1 = sqrt(82)* max( [abs(theo1 - lefts), abs(theo1 - rights)]' )
 pval1 = 1 - cdf_BB(Dn1)

 %Hypothesis 02: Galaxy data has a density which is a  mixture of 5 normal
 %densities:
 %0.1 * normal(9,0.5^2), 0.02* normal(17, sqrt(0.8),
 %0.4 * normal(20, sqrt(5)), 0.4 * normal(23,sqrt(8)), 0.05*normal(33, sqrt(2)).
 theo2 = 0.1* cdf_nor( galaxy, 9, 0.5 ) + ...
  0.02 * cdf_nor( galaxy, 17, sqrt(0.8) ) + ...
  0.4 * cdf_nor( galaxy, 20, sqrt(5) ) + ...
  0.4 * cdf_nor( galaxy, 23, sqrt(8) ) + ...
  0.05 * cdf_nor( galaxy, 33, sqrt(2) );
 figure(3)
 plot( lefts  ,'k.','markersize', msize)
 hold on
 plot(rights ,'k*','markersize', msize)
 plot(theo2,'k--', 'linewidth', 3 )
 axis tight
print -deps 'C:\NPBook\GoF\galaxy3.eps'
 %-------------------------------------------------------------------------
 %-------------------------------------------------------------------------
 Dn2 = sqrt(82)*max( [abs(theo2 - lefts), abs(theo2 - rights)]' )
 pval2 = 1 - cdf_BB(Dn2)